﻿

using Dapper;
using HBase;
using HBase.cache;
using hCommand;

using hCommand.dto;
using hCommand.entity;
using hCommand.mySql;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;

namespace hCommand.abs
{
    public abstract class AbsSysServer : AbsWMSBaseServer, ISysServer
    {

        static long SEQUENCES_ID = 0;
        static int index = 0;
        public long getId()
        {
            lock (this)
            {
                long id = 0;
                if (SEQUENCES_ID == 0 || index == 999)
                {
                    SEQUENCES_ID = querySequence(null, null);
                    index = 0;
                }
                index++;
                id = SEQUENCES_ID * 1000 + index;
                return id;
            }
        }
        public virtual DateTime GetTime()
        {
            return getConnection("").Query<DateTime>("select getdate()").Single();
        }

        public abstract long querySequence(IDbConnection connection, IDbTransaction transaction);
        
        public BParam getParam(string driveType, int driveId, string key)
        {
           
            string sql = "select * from b_Param where drive_Type=@driveType and drive_index=@driveId and key_Code=@key";
            BParam mechineStatus = getConnection(BaseConfig.BaseDBConnStr).QueryUnique<BParam>(sql, new { driveType, driveId, key });
            return mechineStatus;
        }
        public List<BParam> getParams(string driveType, int driveId)
        {
            string sql = "select * from b_Param  where 1=1 ";
            if (!string.IsNullOrWhiteSpace(driveType))
            {

                sql += " and drive_Type=@driveType";
            }

            if (driveId > 0)
            {

                sql += " and drive_index=@driveId";
            }
            return getConnection(BaseConfig.BaseDBConnStr).Query<BParam>(sql, new { driveType, driveId }).ToList();
        }
        public List<BParam> getParams()
        {
            string sql = "select * from b_Param";
            return getConnection(BaseConfig.BaseDBConnStr).Query<BParam>(sql, new { }).ToList();
        }
        public bool update(string driveType, int driveId, string key, long value)
        {
            string sql = "update b_Param set v=@value where  drive_Type=@driveType  and key_Code=@key ";
            if (driveId != 0)
            {
                sql += " and drive_index = @driveId";
            }
            int i = getConnection(BaseConfig.BaseDBConnStr).Execute(sql, new { value, driveType, key, driveId });

        
            if (i != 1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public bool   updateParamCacheTicks(int paramId){
            string sql = "update b_Param set v=@value where drive_type='cache' and id=@paramId   ";
             
            int i = getConnection(BaseConfig.BaseDBConnStr).Execute(sql, new { value= HUtil.getMyDate(DateTime.Now), paramId }); 
            if (i != 1) {
                return false;
            } else {
                return true;
            }
        }
        public List<SysFile> getFile(long pid)
        {
            return getConnection(BaseConfig.BaseDBConnStr).Query<SysFile>("select  * from sys_file where pid=@pid and status=1", new { pid }).ToList();
        }
        public Page<SysFile> queryFilePage(FileDto dto)
        {
            string sql = "select * from sys_file where 1=1 ";
            if (!string.IsNullOrEmpty(dto.tableName))
            {
                sql += $" AND table_Name =@tableName ";
            }
            if (dto.cx > 0 && dto.sblx > 0)
            {
                sql += $" AND type = 'equp" + dto.sblx + dto.cx + "' ";
            }
            else if (dto.sblx > 0)
            {
                sql += $" AND type like 'equp" + dto.sblx + "_' ";
            }
            else if (dto.cx > 0)
            {
                sql += $" AND type like 'equp_" + dto.cx + "' ";
            }
            return queryPage<SysFile>(BaseConfig.BaseDBConnStr, sql, "id", dto);
        }

        public List<SysFile> queryFileList(FileDto dto)
        {
            string sql = "select * from sys_file where 1=1 ";
            if (!string.IsNullOrEmpty(dto.tableName))
            {
                sql += $" AND table_Name =@tableName ";
            }
            sql += $" AND ( type = 'equp" + dto.sblx + dto.cx + "' or type = 'equp" + dto.sblx + "') ";
            return getConnection(BaseConfig.BaseDBConnStr).Query<SysFile>(sql, dto).ToList();
        }

        public void clearFile(long pid)
        {
            getConnection(BaseConfig.BaseDBConnStr).Execute("update sys_file set status=0 where pid=@pid", new { pid });
        }

        public void saveFile(SysFile f)
        {
            getConnection(BaseConfig.BaseDBConnStr).Insert(f);
        }

        public List<T> getChange<T>(long tick) where T : BaseEntity {

            var type = typeof(T);
            var name = ToStr.GetTableName(type);
            string sql = "select * from " + name + " where xg>=@time" ;

            IDbConnection con = getConnection(BaseConfig.BaseDBConnStr);
            List<T> list = con.Query<T>(sql, new { time = tick }).ToList();
            return list;
           
        }
    }
}
